using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Globalization;
using Microsoft.Practices.Repository.Properties;

namespace Microsoft.Practices.Repository
{
    /// <summary>
    /// This exception is thrown when a validation error
    /// is detected in the repository or database.
    /// </summary>
    [Serializable]
    public class RepositoryValidationException : RepositoryException
    {
        private readonly string failedField;
        private readonly string failureReason;

        /// <summary>
        /// Creates a new instance of RepositoryValidationException.
        /// </summary>
        /// <param name="failedField">The name of the database field that did not pass the validation.</param>
        /// <param name="failureReason">The reason why the database field did not pass the validation.</param>
        public RepositoryValidationException(string failedField, string failureReason)
            : this(failedField, failureReason, null)
        {
        }

        /// <summary>
        /// Creates a new instance of RepositoryValidationException.
        /// </summary>
        /// <param name="failedField">The name of the database field that did not pass the validation.</param>
        /// <param name="failureReason">The reason why the database field did not pass the validation.</param>
        /// <param name="inner">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
        public RepositoryValidationException(string failedField, string failureReason, Exception inner)
            : base(string.Format(CultureInfo.CurrentCulture, Microsoft.Practices.Repository.Properties.GenericResources.ValidationFailed, failedField, failureReason), inner)
        {
            this.failedField = failedField;
            this.failureReason = failureReason;
        }

        /// <summary>
        /// Gets the name of the field that did not pass the validation.
        /// </summary>
        public string FailedField
        {
            get { return failedField; }
        }

        /// <summary>
        /// Gets the reason why the field did not pass the validation.
        /// </summary>
        public string FailureReason
        {
            get { return failureReason; }
        }

        /// <summary>
        /// Creates a new instance of RepositoryVaidationException.
        /// </summary>
        public RepositoryValidationException()
        {
        }

        /// <summary>
        /// Creates a new instance of RepositoryValidationException.
        /// </summary>
        /// <param name="message">The message that explains the reason for the exception.</param>
        public RepositoryValidationException(string message)
            : base(message)
        {
        }

        /// <summary>
        /// Creates a new instance of RepositoryValidationException.
        /// </summary>
        /// <param name="message">The message that explains the reason for the exception.</param>
        /// <param name="inner">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
        public RepositoryValidationException(string message, Exception inner)
            : base(message, inner)
        {
        }

        /// <summary>
        /// Creates a new instance of RepositoryValidationException.
        /// </summary>
        /// <param name="info">The System.Runtime.Serialization.SerializationInfo that holds the serialized data
        /// about the exception being thrown.</param>
        /// <param name="context">The System.Runtime.Serialization.SerializationContext that contains contextual informationa about
        /// the source or destination.</param>
        protected RepositoryValidationException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    }
}
